package com.wiloon.java8x.thread.CyclicBarrier;

import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by wiloon on 6/1/17;
 */
public class CyclicBarrierX {
    public static void main(String[] args) {
        int PLAYER_COUNT = 3;
        CyclicBarrier barrier = new CyclicBarrier(PLAYER_COUNT, new Runnable() {

            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName() + " execute other task.");
            }
        });
        People[] peoples = new People[PLAYER_COUNT];
        for (int i = 0; i < PLAYER_COUNT; i++) {
            peoples[i] = new People(i, barrier);
        }
        ExecutorService exe = Executors.newFixedThreadPool(PLAYER_COUNT);

        //这里如果人数小于PLAYER_COUNT，将会一直等待
        for (People people : peoples) {
            exe.execute(people);
        }
        exe.shutdown();
    }
}
